<template>
	<view>
		<view>当前batchId：{{ batchId }}</view>
		<input placeholder="设置分享的批次ID" v-model="batchIdGroup" />
		<button @click="handleKeepBatchId">保存当前的批次ID</button>
		<button @click="changeBatchId">分享携带批次号:{{ shareWithBatchId ? '已开启' : '已关闭' }}</button>
		<button @click="copyBatchId">复制batchId</button>
		<button @click="openCzPage">抖音一键加群授权</button>
		<view v-if="showCzPage" class="mask">
			<view class="mask-content">
				<view class="mask-title">
					输入抖音id
					<input class="mask-input" v-model="userId" />
				</view>
				<view>
					<radio-group  class="ttui-cells">
						<label class="ttui-cell" v-for="(item, index) in list"  @click="checkboxChange(item)" :key="index">
							<view class="ttui-cell__hd">
								<radio :value="item.value" :checked="item.checked" :disabled="false" color="#F85959" />
							</view>
							<view class="ttui-cell__bd">{{ item.name }}</view>
						</label>
					</radio-group>
				</view>
				<button class="mask-btn" @click="jumpActivityPage">确定</button>
			</view>

		</view>
	</view>
</template>



<script>
import moneyPopupTT from '../../components/moneyPopupTT.vue'

export default {
	components: {
		moneyPopupTT
	},
	data() {
		return {
			batchIdGroup: '',
			shareWithBatchId: getApp().globalData.shareWithBatchId,
			batchId: '',
			nowBatchId: '',
			showCzPage: false,
			userId: '',
			type: '',
			list: [
				{
					value: 1,
					name: "私域运营",
				},
				{
					value: 2,
					name: "短视频运营",
				},
			],
			appId: '',
			ticket:''
		}
	},
	onLoad() {
		let ttAccountInfo = tt.getEnvInfoSync() && tt.getEnvInfoSync().microapp
		this.appId = ttAccountInfo.appId
		console.log(ttAccountInfo, 'ttConfigMess ? ttConfigMess.appId');
		this.nowBatchId = uni.getStorageSync('nowBatchId') || ''
		// this.shareWithBatchId = wx.getStorageSync('shareWithBatchId') || false
		this.shareWithBatchId = getApp().globalData.shareWithBatchId || false
		if(this.shareWithBatchId){
			wx.setStorageSync('shareWithBatchId', this.shareWithBatchId);
		}
		this.batchId = getApp().globalData['batchId']
	},
	onUnload() {
	},
	onShow() {
	},
	methods: {
		// 
		openCzPage() {
			this.showCzPage = true
		},
		checkboxChange(e) {
			this.type = e.value;
			console.log(e);
		},
		jumpActivityPage() {
			let _this = this
			tt.showDouyinOpenAuth({
				scopes: {
					// "user_info": 1,
					"data.external.user": 1,
					"im.group_fans.create_list": 1,
					"im.group_fans.share": 1,
					"tool.image.upload":1
				},
				success(res) {
					_this.ticket = res.ticket
					console.log(_this.ticket,'this.ticket------this.ticket');
					tt.getUserInfo({
						withCredentials: true,
						success(resUse) {
							console.log(`getUserInfo 调用成功 `,resUse.userInfo);
							_this.addMess(resUse.userInfo)
						},
						fail(res) {
							console.log(`getUserInfo 调用失败`);
						},
					});
					
					console.log("success", res);
				},
				fail(res) {
					console.log("fail", res);
				},
				complete(res) {
					console.log("complete", res);
				},
			});


		},
			// 抖音授权
		 addMess(resUse) {
			try {
				this.zwyRequest({
					url: `/douYin/account/getAuthority`,
					method: "POST",
					data: {
						'appId': this.appId,
						'avatar': resUse.avatarUrl,
						'code': this.ticket,
						'nickname': resUse.nickName,
						'type': this.type,
						'userId': this.userId
					}
				}).then((res) => {
					console.log(res, "res11111111");
				});
			} catch (error) {
				console.log(error)
			}
		},
		// 设置固定批次ID并开启分享携带
		handleKeepBatchId() {
			if (!this.batchIdGroup) {
				uni.showToast({
					title: `请输入批次ID`,
					icon: 'none'
				})
				return
			}

			uni.showToast({
				title: `保存成功`,
				icon: 'none'
			})
			getApp().globalData.batchId = this.batchIdGroup
			uni.setStorageSync('nowBatchId', this.batchIdGroup)
			this.shareWithBatchId = true
			getApp().globalData.shareWithBatchId = true
			wx.setStorageSync('shareWithBatchId', this.shareWithBatchId);
			setTimeout(() => {
				uni.navigateBack({
					delta: 1,
					fail: res => {
						uni.switchTab({
							url: '/pages/fx_xjc/fx_xjc'
						});
					}
				})
			}, 2000)
		},
		copyBatchId() {
			uni.setClipboardData({
				data: this.batchId
			})
		},
		changeBatchId() {
			this.shareWithBatchId = !this.shareWithBatchId;
			getApp().globalData.shareWithBatchId = !getApp().globalData.shareWithBatchId
			wx.setStorageSync('shareWithBatchId', this.shareWithBatchId);
		}
	}
}
</script>

<style lang="scss" scoped>
.ttui-cells{
	display: flex;
	justify-content: space-between;
}
.ttui-cell{
	display: flex;
}
.line-mess {
	display: flex;
	align-items: flex-start;
	flex-wrap: wrap;
	margin: 12rpx 0;
}

.right-box {
	flex: 1;
	word-break: break-all;
	margin-left: 24rpx;
	font-weight: bold;
}

.mask {
	width: 560rpx;
	height: 360rpx;
	border-radius: 20rpx;
	background: rgba($color: #000000, $alpha: 0.4);
	margin: 0 auto;
	position: fixed;
	left: 0;
	right: 0;
	top: 200rpx;
	bottom: 0;

}

.mask-content {
	background: #fff;
	padding-bottom: 80rpx;
	padding: 30rpx;
}

.mask-title {
	padding-bottom: 30rpx;
	// padding: 30rpx;

}

.mask-input {
	border: 1rpx solid #ccc;
	padding: 10rpx;
}

.mask-btn {
	width: 160rpx;
	height: 50rpx;
	line-height: 50rpx;
	font-size: 28rpx;
	margin-top: 100rpx;
}
</style>
